package kata02;

public class InterpolationChop {
    int[] items;

    public InterpolationChop(int[] items) {
        this.items = items;
    }

    public int findIndex (int element) {
        return findIndex(0, items.length - 1, element);
    }

    public int findIndex(int left, int right, int element) {
        if (left > right || element < items[0] || element > items[items.length - 1])
            return -1;
        int middle = left;

        if (left != right) {
            middle = left + (element - items[left]) * (right - left) / (items[right] - items[left]);
        }
        int middleValue = items[middle];
        if (element > middleValue)
            return findIndex(middle + 1, right, element);
        else if (element < middleValue)
            return findIndex(left, middle - 1, element);
        else {
            return middle;
        }
    }
}
